package com.kuang.unsafe;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

/**
 * @program: juc
 * @description:
 * @author: 陈恩涛
 * @create: 2021-02-02 15:05
 **/
/*
* 同理可证： ConcurrentModificationException
* 解决方案：
*   1、Set<String> set = Collections.synchronizedSet(new HashSet<>());
*   2、CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<>();
* */
public class SetTest {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        //Set<String> set = Collections.synchronizedSet(new HashSet<>());
        //CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<>();

        for (int i = 1; i <= 30; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(set);
            },String.valueOf(i)).start();
        }

    }
}
